package bloomfilterdemo;

import java.util.BitSet;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-03
 * Time: 20:09
 */
class SimpleHash2{
    public int cap;
    public int seed;
    public SimpleHash2(int cap ,int seed){
        this.cap = cap;
        this.seed = seed;
    }
    public int hash(String key){
        int h;
        //(n - 1) & hash
        return (key == null) ? 0 : (seed * (cap-1)) & ((h = key.hashCode()) ^ (h >>> 16));
    }

}
public class MyBloomFilter2 {
    public static final int DEFAULT_SIZE = 1<<20;
    public BitSet bitSet;
    public int usedSize;
    public static final int[] seeds = {1,3,5,7,8,33,27};
    public SimpleHash2[] simpleHashes2;
    public MyBloomFilter2() {
        bitSet = new BitSet();
        simpleHashes2 = new SimpleHash2[seeds.length];
    }
    public void set(String key){
        for (int i = 0; i < simpleHashes2.length; i++) {
            int index = simpleHashes2[i].hash(key);
            bitSet.set(index);
        }
    }
    public boolean get(String key){
        for (int i = 0; i <simpleHashes2.length ; i++) {
            int index = simpleHashes2[i].hash(key);
            if(!bitSet.get(index)){
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        MyBloomFilter myBloomFilter = new MyBloomFilter();
        myBloomFilter.add("hello");
        myBloomFilter.add("hello2");
        myBloomFilter.add("bit");
        myBloomFilter.add("haha");

        System.out.println(myBloomFilter.contains("hello"));
        System.out.println(myBloomFilter.contains("hello3"));
        System.out.println(myBloomFilter.contains("he"));
    }

}

